#region license
# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""Standalone disposable testing."""

namespace Grawlix.Meta.Tests.Disposable
import Grawlix.Meta
import NUnit.Framework
import System

[Disposable]
class Standalone(IDisposeImpl):
	
	event NumEvent as System.EventHandler[of System.EventArgs]
	
	def RaiseEvent(val as int):
		NumEvent(self, NumberArgs(val))
	
	def Double(x as int):
		return x + x
	
	def No0([required(x != 0)] x as int):
		return x
	
	override def ToString():
		return _text
	
	[getter(Number)]
	private _number = 15
	
	[property(Text)]
	private _text = "hello world!"

[Disposable(ForceFinalizer: true)]
class StandaloneFinalizable(IDisposeImpl):
	
	event NumEvent as System.EventHandler[of System.EventArgs]
	
	def RaiseEvent(val as int):
		NumEvent(self, NumberArgs(val))
	
	def Double(x as int):
		return x + x
	
	def No0([required(x != 0)] x as int):
		return x
	
	override def ToString():
		return _text
	
	[getter(Number)]
	private _number = 15
	
	[property(Text)]
	private _text = "hello world!"
	
	

[TestFixture]
class StandaloneFixture:
	
	type = Standalone
	
	[Test]
	def NonDisposedIntegrity():
		obj = type()
		DisposeTests.Integrity(obj)
	
	[Test]
	def DisposedInvalidUse():
		obj = type() as IDisposable
		obj.Dispose()
		DisposeTests.InvalidUse(obj)
	
	[Test]
	def DisposedRepeat():
		obj = type() as IDisposable
		obj.Dispose()
		DisposeTests.Repeated(obj)
	
	
[TestFixture]
class StandaloneFinalizableFixture:
	
	type = StandaloneFinalizable
	
	[Test]
	def NonDisposedIntegrity():
		obj = type()
		DisposeTests.Integrity(obj)
	
	[Test]
	def DisposedInvalidUse():
		obj = type() as IDisposable
		obj.Dispose()
		DisposeTests.InvalidUse(obj)
	
	[Test]
	def DisposedRepeat():
		obj = type() as IDisposable
		obj.Dispose()
		DisposeTests.Repeated(obj)
	
	[Test]
	def FinalizedInvalidUse():
		obj = DisposeTests.DoFinalize(type())
		expect obj is not null
		DisposeTests.InvalidUse(obj as IDisposeImpl)
	
	[Test]
	def FinalizedRepeat():
		obj = DisposeTests.DoFinalize(type())
		expect obj is not null
		DisposeTests.Repeated(obj as IDisposeImpl)
